﻿using CommonHelper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Order.MicroService.IService;
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;

namespace Order.MicroService.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(IgnoreApi = true)]
    public class TestController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<TestController> _logger;

        private readonly IOrderService _orderService;

        public TestController(ILogger<TestController> logger, IOrderService orderService)
        {
            _logger = logger;
            _orderService = orderService;
        }

        [HttpPost("Login")]
        public IActionResult Login([Required] string UserName, [Required] string Password)
        {
            _logger.LogError("测试封装日志");
            int i = 0;
            int result = 10 / i;
            return Ok(UserName);
        }
        [HttpPost("GetResult")]
        public ApiResult<string> GetResult([Required] string name)
        {
            return ApiResult<string>.FailResult(name);
        }

        //[HttpGet("DoSomething")]
        //public async void DoSomething()
        //{
        //    Console.WriteLine("异步方法开始1");
        //    //通过第一个异步方法得到结果

        //    var aaa = await Task.Run(() =>
        //    {
        //        Thread.Sleep(5000);
        //        return 100;
        //    });
        //    Console.WriteLine("结果是{0}", aaa);
        //}

        [HttpGet("DoSomething2")]
        public void TestNullOr()
        {
            int? myInt1 = null;

            Console.WriteLine($"{myInt1 ?? -1}");

            myInt1 = 14;
            Console.WriteLine($"{myInt1 ?? -1}");

            int? x = null; //定义可空类型变量
            int? y = x ?? 100; // x ?? 100等价于 x==null?100:x
            
            Console.WriteLine(y.ToString());
        }

        [HttpGet("GetOrderInfo")]
        public ApiResult<string> GetOrderInfo(string name)
        {
            return ApiResult<string>.SuccessResult(_orderService.GetOrderInfo(name));
        }
    }
}
